package io.renren.modules.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.basic.dao.MtCcInventoryInfoDao;
import io.renren.modules.basic.dao.MtCcWarehousPositionRelatDao;
import io.renren.modules.basic.entity.MtCcInventoryInfoEntity;
import io.renren.modules.basic.entity.MtCcWarehousPositionRelatEntity;
import io.renren.modules.work.dao.WorkDao;
import io.renren.modules.work.entity.WorkEntity;
import io.renren.modules.work.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: weihongyang
 * @Date: 2019/10/14 10:22
 * @Version 1.0
 */
@Service("workService")
public class WorkServiceImpl extends ServiceImpl<WorkDao,WorkEntity> implements WorkService {

    @Autowired
    private MtCcWarehousPositionRelatDao mtCcWarehousPositionRelatDao;

    @Autowired
    private MtCcInventoryInfoDao mtCcInventoryInfoDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String checkOutForMainTask(Map<String, Object> params) {
        /**
         * 1.修改总任务表中状态字段
         * 未执行改变成执行中
         */
        MtCcInventoryInfoEntity mtCcInventoryInfoEntity = new MtCcInventoryInfoEntity();
        mtCcInventoryInfoEntity.setInventoryStatus(1);
        mtCcInventoryInfoDao.update(mtCcInventoryInfoEntity,new QueryWrapper<MtCcInventoryInfoEntity>().eq("task_code",
                params.get("taskCode")));
        /**
         * 2.按照货物类型数量查找仓库
         */
        List<MtCcWarehousPositionRelatEntity> list = mtCcWarehousPositionRelatDao.selectNumForCheckOut(params);
        int num = 0;
        int checkOutNum = Integer.parseInt(params.get("checkOutNum").toString());
        List<MtCcWarehousPositionRelatEntity> relatEntityList = new ArrayList<>();
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:list) {
            num = num + mtCcWarehousPositionRelatEntity.getQuantity();
            relatEntityList.add(mtCcWarehousPositionRelatEntity);
            if (num >= checkOutNum) {
                break;
            }
        }
        /**
         * 判断仓库是否满足出库的数量
         */
//        if (num <= checkOutNum) {
//            return "仓库中"+params.get("productName")+"数量为"+num+",你要出库的数量为"+checkOutNum+",仓库数量不够出库！";
//        }

        /**
         * 把总任务分配详细任务
         */
        for (MtCcWarehousPositionRelatEntity mtCcWarehousPositionRelatEntity:relatEntityList) {

        }



        return "";
    }
}
